import 'package:flutter/services.dart';


class Bridge {
  BasicMessageChannel _userInfoMessageChannel = BasicMessageChannel('Account.UserInfo', StandardMessageCodec());
  MethodChannel _refreshUserInfomethodChannel = MethodChannel('Account.RefreshUserInfo');
  EventChannel _userPositionEventChannel = EventChannel("Account.UserPosition");

// MessageChannel
  Future sendUserInfoMessage(dynamic message) async {
    var result = await _userInfoMessageChannel.send(message);
    print('flutter send message to native');
    return Future.value(result);
  }

 Future setUserInfoMessageHandle() async {
    Future<String> messageHandle(parama) async {
      print('native call flutter ===== $parama');
      return 'messageHandle flutter return';
    }
    _userInfoMessageChannel.setMessageHandler(messageHandle);
  }

// MethodChannel
  Future callRefreshUserInfomethodChannelMethod([dynamic arguments]) async {
    var result;
    try {
      result = await _refreshUserInfomethodChannel.invokeMethod('refreshUserInfo',arguments);
      print('$result');
      return Future.value(result);
    } catch (e) {
      return Future.error(e.toString());
    }
 
  }

  Future setRefreshUserInfomethodChannelMethodHandle() async {
    Future<String> methodHandle(MethodCall call) async {
      print('nativeCallFlutter_result');
      return '回调结果';
    }
    _refreshUserInfomethodChannel.setMethodCallHandler(methodHandle);
  }

// EventChanel
  void _userPositionOnEvent(Object event) {
    print('_onEvent = $event');
  }

  void _userPositionOnError(Object error){
      print('_onError = $error');
  }

  void _userPositionOnDone() {
      print('_onDone');
  }

  Future userPositionEventChannel() async {
    _userPositionEventChannel.receiveBroadcastStream().listen(_userPositionOnEvent, onError: _userPositionOnError ,onDone: _userPositionOnDone,);
  }
}